const { logsModel } = require('./models/logsModel');

// const moment = require('moment');
// logsModel.find({
//     handleTime: {
//         "$gte": new Date('2020-11-12'),
//         "$lte": new Date('2020-12-16')
//     }
// })


// 登录
module.exports.logs = async function (params) {
    // 总条数
    const total = (await logsModel.find()).length;
    const pages = Math.ceil(total / params.pageSize);
    const data = await logsModel.find()
        .limit(params.pageSize - 0)//设置请求的条数
        .skip((params.currentPage - 1) * params.pageSize);//跳过数据的条数
    return {
        data, total, pages
    };
}
// 删除
module.exports.deleteLog = async function ({ msg }) {
    return await logsModel.deleteOne(msg);
}
// 新增
module.exports.addLogsMsg = async function (msg) {
    return await logsModel.create(msg);
}
// 搜索：seekLogsUser,seekLogsResult,seekLogsHandle,seekLogsTime
module.exports.seekLogsUser = async function (msg) {
    // 总条数
    const total = (await logsModel.find({ username: { $regex: msg.username, $options: '$i' } })).length;
    const pages = Math.ceil(total / msg.pageSize);
    const data = await logsModel.find({ username: { $regex: msg.username, $options: '$i' } })
        .limit(msg.pageSize - 0)//设置请求的条数
        .skip((msg.currentPage - 1) * msg.pageSize);//跳过数据的条数;
    return {
        data, total, pages
    }
}
module.exports.seekLogsResult = async function (msg) {
    // 总条数
    console.log(msg, "2");
    const total = (await logsModel.find({ result: { $regex: msg.result, $options: '$i' } })).length;
    const pages = Math.ceil(total / msg.pageSize);
    const data = await logsModel.find({ result: { $regex: msg.result, $options: '$i' } })
        .limit(msg.pageSize - 0)//设置请求的条数
        .skip((msg.currentPage - 1) * msg.pageSize);//跳过数据的条数;
    return {
        data, total, pages
    }
}
module.exports.seekLogsHandle = async function (msg) {
    // 总条数

    const total = (await logsModel.find({ handleType: { $regex: msg.handleType, $options: '$i' } })).length;
    const pages = Math.ceil(total / msg.pageSize);
    const data = await logsModel.find({ handleType: { $regex: msg.handleType, $options: '$i' } })
        .limit(msg.pageSize - 0)//设置请求的条数
        .skip((msg.currentPage - 1) * msg.pageSize);//跳过数据的条数;
    return {
        data, total, pages
    }
}
module.exports.seekLogsTime = async function (msg) {
    // 总条数
    const total = (await logsModel.find({ handleTime: { $regex: msg.handleTime, $options: '$i' } })).length;
    const pages = Math.ceil(total / msg.pageSize);
    const data = await logsModel.find({ handleTime: { $regex: msg.handleTime, $options: '$i' } })
        .limit(msg.pageSize - 0)//设置请求的条数
        .skip((msg.currentPage - 1) * msg.pageSize);//跳过数据的条数;
    return {
        data, total, pages
    }
}